import 'package:dustbin_sort/pages/login_page.dart';
import 'package:dustbin_sort/util/navigator_util.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyPage extends StatefulWidget
{
	@override
	State<StatefulWidget> createState()=>_MyPageState();

}
class _MyPageState extends State<MyPage>
{
	String nickname = "未登录";
	String username;
	final PageController _controller = PageController(
		initialPage: 4,
	);

	@override
  void initState() {
    // TODO: implement initState
		_init_data();
    super.initState();
  }
  Future _init_data() async
	{
		SharedPreferences preferences = await SharedPreferences.getInstance();
		if(preferences.get("username") == null)
			{
				setState(() {
					nickname = "未登录";
				});
				return;
			}
		username = preferences.get("username");
		nickname = preferences.get("nickname");
		setState(() {

		});
	}
	@override
	Widget build(BuildContext context) {
		Future _login() async{
			SharedPreferences preferences = await SharedPreferences.getInstance();
			print(preferences.get("token"));
			if(preferences.get("isLogin") == true)
				{
					return;
				}else{
				Navigator.push(
						context, MaterialPageRoute(builder: (context) => LoginScreen())
				).then((context){
					_init_data();
				});
			}
		}

		return Scaffold(
			body: Stack(
				children: <Widget>[
				MediaQuery.removePadding(
				removeTop: true,
				context: context,
					child: Column(
					children: <Widget>[
						Container(
							decoration: BoxDecoration(
									gradient: LinearGradient(
											colors: [
												Color.fromARGB(255,94,106,217),
												Color.fromARGB(255,118,178,242),
											],
											begin: Alignment.centerLeft,
											end: Alignment.centerRight
									)
							),
							height: 220,
							child: Padding(padding: EdgeInsets.all(20),child:Container(
								child: Row(
									children: <Widget>[
										Expanded(
												flex: 3,
												child: GestureDetector(
													child: Container(
														decoration: BoxDecoration(
																border: Border.all(color: Colors.blue[200],width: 2),
																shape: BoxShape.circle
														),
														child: new ClipOval(
															child: new Image.asset('assets/images/avator.jpeg'),
														),
													),
													onTap: (){
														_login();
													},
												)),
										Expanded(
											flex: 5,
											child: Column(
												children: <Widget>[
													Expanded(
															child: new Container(
																	alignment: Alignment.center,
																	child: Column(
																		mainAxisSize: MainAxisSize.min,
																		children: <Widget>[
																			Text(nickname, style: TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.w300),),
																			Container(
																				margin: EdgeInsets.only(top: 2),
																				padding: EdgeInsets.all(4),
																				decoration: BoxDecoration(
																						border: new Border.all(color: Colors.white, style: BorderStyle.solid),
																						borderRadius: new BorderRadius.circular(10)
																				),
																				child: Text("158****9469", style: TextStyle(color: Colors.white, fontWeight: FontWeight.w300,fontSize: 12))
																				,
																			)
																		],
																	)
															)
													)
												],
											),
										),
										Expanded(
												flex: 6,
												child: Container(
													child: Row(
														textDirection: TextDirection.rtl,
														children: <Widget>[
															Expanded(
																flex: 1,
																child: Container(
																	child: Center(
																		child: Icon(Icons.insert_comment, color: Colors.white,),
																	),
																),
															),
															Expanded(
																flex: 1,
																child: GestureDetector(
																	child: Container(
																		child:Icon(Icons.settings, color: Colors.white,),
																	),
																	onTap: () async {
																		print("logout");
																		SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
																		//全部清空
																		sharedPreferences.clear();
																		_init_data();
																	},
																),
															),
															Expanded(
																flex: 2,
																child: Container(
																),
															)
														],
													),
												)),
									],
								),
							),)
						),
					],
				),
			),
					Container(
						decoration: BoxDecoration(
							borderRadius: BorderRadius.circular(20),
							color: Colors.white
						),
						margin: EdgeInsets.fromLTRB(0, 180, 0, 0),
						child: ListView(
							children: <Widget>[
								Row(
									children: <Widget>[
										Expanded(
											child: Container(
												height: 80,
												margin: EdgeInsets.fromLTRB(15, 0, 15, 15),
												decoration: BoxDecoration(
													color: Colors.white,
													borderRadius: BorderRadius.circular(6),
													boxShadow: [
														BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),blurRadius: 2,),
														BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 2),
														BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 2),
														BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 2)
													]
												),
												child: Container(
													child: Row(
														children: <Widget>[
															Expanded(
																child: Container(
																	child: Column(
																		children: <Widget>[
																			Expanded(
																				flex: 1,
																				child: Container(
//																				color: Colors.grey,
																					padding: EdgeInsets.only(left: 15),
																					alignment: Alignment.centerLeft,
																					child: Text("搜索记录"),
																				),
																			),
																			Expanded(
																				flex: 1,
																				child: Container(
//																					color: Colors.grey,
																					padding: EdgeInsets.only(left: 15),
																					alignment: Alignment.centerLeft,
																					child: Row(
																						children: <Widget>[
																							Text("40", style: TextStyle(color: Colors.red),),
																							Text(" 垃圾搜索",style: TextStyle(fontSize: 12),)
																						],
																					),
																				),
																			),
																		],
																	),
																),
																flex: 2,
															),
															Expanded(
																child: Container(
																	child: Container(
																		padding: EdgeInsets.only(top: 15),
																		alignment: Alignment.topCenter,
																		child: Icon(Icons.history),
																	),
																),
																flex: 1,
															),
														],
													),
												),

											),
											flex: 1,
										),
										Expanded(
											child: Container(
												height: 80,
												margin: EdgeInsets.fromLTRB(15, 0, 15, 15),

												decoration: BoxDecoration(
														color: Colors.white,
														borderRadius: BorderRadius.circular(6),
														boxShadow: [
															BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),blurRadius: 2,),
															BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 2),
															BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 2),
															BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 2)
														]
												),
												child: Container(
													child: Row(
														children: <Widget>[
															Expanded(
																child: Container(
																	child: Column(
																		children: <Widget>[
																			Expanded(
																				flex: 1,
																				child: Container(
//																				color: Colors.grey,
																					padding: EdgeInsets.only(left: 15),
																					alignment: Alignment.centerLeft,
																					child: Text("回收订单"),
																				),
																			),
																			Expanded(
																				flex: 1,
																				child: Container(
//																					color: Colors.grey,
																					padding: EdgeInsets.only(left: 15),
																					alignment: Alignment.centerLeft,
																					child: Row(
																						children: <Widget>[
																							Text("5", style: TextStyle(color: Colors.red),),
																							Text(" 个回收",style: TextStyle(fontSize: 12),)
																						],
																					),
																				),
																			),
																		],
																	),
																),
																flex: 2,
															),
															Expanded(
																child: Container(
																	child: Container(
																		padding: EdgeInsets.only(top: 15),
																		alignment: Alignment.topCenter,
																		child: Icon(Icons.restore_from_trash,),
																	),
																),
																flex: 1,
															),
														],
													),
												),

											),
											flex: 1,
										),
									],
								),
								Column(
									children: <Widget>[
										Container(
											margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
											decoration: BoxDecoration(
													border: new Border(bottom:BorderSide(color: Colors.grey, style: BorderStyle.solid))
											),
											padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
											child: Row(
												mainAxisSize: MainAxisSize.max,
												children: <Widget>[
													Container(
														child: Text("隐私政策"),
													),
													Expanded(
															flex: 1,
															child: Container(
																	alignment: Alignment.centerRight,
																	child: Icon(Icons.chevron_right))
													),
												],
											),
											height: 40,
											alignment: Alignment.centerLeft,
										),
										Container(
											margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
											decoration: BoxDecoration(
													border: new Border(bottom:BorderSide(color: Colors.grey, style: BorderStyle.solid))
											),
											padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
											child: Row(
												mainAxisSize: MainAxisSize.max,
												children: <Widget>[
													Container(
														child: Text("分享好友"),
													),
													Expanded(
															flex: 1,
															child: Container(
																	alignment: Alignment.centerRight,
																	child: Icon(Icons.chevron_right))
													),
												],
											),
											height: 40,
											alignment: Alignment.centerLeft,
										),
										Container(
											margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
											decoration: BoxDecoration(
													border: new Border(bottom:BorderSide(color: Colors.grey, style: BorderStyle.solid))
											),
											padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
											child: Row(
												mainAxisSize: MainAxisSize.max,
												children: <Widget>[
													Container(
														child: Text("五星好评"),
													),
													Expanded(
															flex: 1,
															child: Container(
																	alignment: Alignment.centerRight,
																	child: Icon(Icons.chevron_right))
													),
												],
											),
											height: 40,
											alignment: Alignment.centerLeft,
										),
										Container(
											margin: EdgeInsets.fromLTRB(20, 20, 20, 0),
											decoration: BoxDecoration(
													border: new Border(bottom:BorderSide(color: Colors.grey, style: BorderStyle.solid))
											),
											padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
											child: Row(
												mainAxisSize: MainAxisSize.max,
												children: <Widget>[
													Container(
														child: Text("意见反馈"),
													),
													Expanded(
															flex: 1,
															child: Container(
																	alignment: Alignment.centerRight,
																	child: Icon(Icons.chevron_right))
													),
												],
											),
											height: 40,
											alignment: Alignment.centerLeft,
										),
									],
								)


							],
						),
					)
			]
		)
		);
	}

}